﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class DelProductionTaskCommandHanders : IRequestHandler<DelProductionTaskCommands, APIResult<ProductionTask>>
    {
        private readonly IBaseRepository<ProductionTask> baseRepository;
        private readonly IMapper mapper;

        public DelProductionTaskCommandHanders(IBaseRepository<ProductionTask> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除生产任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<ProductionTask>> Handle(DelProductionTaskCommands request, CancellationToken cancellationToken)
        {
            APIResult<ProductionTask> result= new APIResult<ProductionTask>();
            result.Code = APIEnum.删除成功;
            result.Msg = "删除成功";
            var cao = mapper.Map<ProductionTask>(request);
            var sql = $"Update ProductionTask set IsDelete=true where ProductionTaskId='{request.ProductionTaskId}'";
            await baseRepository.Update(cao, sql);
            return result;
        }
    }
}
